package com.zhanghang.vending_machine.controller;

import com.zhanghang.vending_machine.common.result.PageResult;
import com.zhanghang.vending_machine.common.result.Result;
import com.zhanghang.vending_machine.domain.dto.GoodPageDto;
import com.zhanghang.vending_machine.domain.po.Category;
import com.zhanghang.vending_machine.domain.po.Good;
import com.zhanghang.vending_machine.domain.vo.GoodVo;
import com.zhanghang.vending_machine.service.CategoryService;
import com.zhanghang.vending_machine.service.GoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "基础页面管理接口")
@CrossOrigin(origins = "*")
public class UserController {
    private final GoodService goodService;
    private final CategoryService categoryService;

    @GetMapping("/goods/page")
    @ApiOperation("分页查询商品信息")
    public Result<PageResult> goodPage(GoodPageDto goodPageDto){
        log.info("条件分页：{}",goodPageDto);

        PageResult pageResult = goodService.PageByCategory(goodPageDto.getCategoryId(),goodPageDto.getPageNo(),goodPageDto.getPageSize());

        return Result.success(pageResult);
    }

    @GetMapping("/goods/{id}")
    @ApiOperation("根据id查询商品信息")
    public Result<GoodVo> getGoodById(@PathVariable Long id){
        log.info("根据id：{}，查询商品信息",id);

        GoodVo goodVo = goodService.getGoodById(id);

        return Result.success(goodVo);
    }

    @GetMapping("/category")
    public Result<List<Category>> getCategory(){
        List<Category> list = categoryService.list();
        return Result.success(list);
    }
}
